package kr.hkit.androidcomplete02.ch16;

import kr.hkit.androidcomplete02.R;
import kr.hkit.androidcomplete02.R.array;
import kr.hkit.androidcomplete02.ch16.QuestionDialgFragment.NoticeDialogListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class RadioDialogFrg extends Activity implements ListDialogFragment.NoticeDialogListener{
	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_dialog);
		
		tv =(TextView) findViewById(R.id.ch16_dialog_tv);
		
	}
	
	public void mOnClick(View v ){
		ListDialogFragment listFrg = new ListDialogFragment();
		listFrg.show(getFragmentManager(), "dialog");
	}

	@Override
	public void onDialogClick(DialogFragment frg, String res) {
		// TODO Auto-generated method stub
		tv.setText("선택한 과일은 : " + res);
	}
}

class ListDialogFragment extends DialogFragment{
	NoticeDialogListener mListener;
	int mSelect = 0;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try{
			mListener = (NoticeDialogListener) activity;
		}catch(ClassCastException e){
			throw new ClassCastException(activity.toString() + " NoticeDialogListener 를 반드시 구현");
		}
		
	}
	
	public interface NoticeDialogListener{
		public void onDialogClick(DialogFragment frg, String res);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return new AlertDialog.Builder(getActivity())
				.setTitle("음식을 선택하세요")
				.setIcon(R.drawable.gang4)
				.setSingleChoiceItems(R.array.fruits, mSelect, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						mSelect = which;
					}
				})
				.setPositiveButton("확인", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						String fruits[] = getResources().getStringArray(R.array.fruits);
						mListener.onDialogClick(ListDialogFragment.this, fruits[mSelect]);
						
					}
				})
				.setNegativeButton("닫기", null)
				.create();
				
				
	}
}